<?php
  @header("content-Type: text/html; charset=utf-8"); //语言强制
  $lujing = str_replace('/install', '', dirname(__dir__));

  error_reporting(E_ALL);
/**
 * 转化 \ 为 /
 * 
 * @param	string	$path	路径
 * @return	string	路径
 */
function dir_path($path)
{
    $path = str_replace('\\', '/', $path);
    if (substr($path, -1) != '/')
        $path = $path . '/';
    return $path;
}

/**
 * 创建目录
 * 
 * @param	string	$path	路径
 * @param	string	$mode	属性
 * @return	string	如果已经存在则返回true，否则为flase
 */
function dir_create($path, $mode = 0777)
{
    if (is_dir($path))
        return true;
    $ftp_enable = 0;
    $path = dir_path($path);
    $temp = explode('/', $path);
    $cur_dir = '';
    $max = count($temp) - 1;
    for ($i = 0; $i < $max; $i++) {
        $cur_dir .= $temp[$i] . '/';
        if (@is_dir($cur_dir))
            continue;
        @mkdir($cur_dir, 0777, true);
        @chmod($cur_dir, 0777);
    }
    return is_dir($path);
}

/**
 * 拷贝目录及下面所有文件
 * 
 * @param	string	$fromdir	原路径
 * @param	string	$todir		目标路径
 * @return	string	如果目标路径不存在则返回false，否则为true
 */
function dir_copy($fromdir, $todir)
{
    $fromdir = dir_path($fromdir);
    $todir = dir_path($todir);
    if (!is_dir($fromdir))
        return false;
    if (!is_dir($todir))
        dir_create($todir);
    $list = glob($fromdir . '*');
    if (!empty($list)) {
        foreach ($list as $v) {
            $path = $todir . basename($v);
            if (is_dir($v)) {
                dir_copy($v, $path);
            } else {
                copy($v, $path);
                @chmod($path, 0777);
            }
        }
    }
    return true;
}





/**
 * @取得当前URL路径
 * 
 * 用了协助信息的返回
 */
  function url() {

    $pageURL = 'http';
    if (@$_SERVER["HTTPS"] == "on") {
      $pageURL .= "s";
    }
    
    
    $pageURL .= "://";
    if ($_SERVER["SERVER_PORT"] != "80") {
      $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
    } else {
      $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    }
    return $pageURL;
  }



  /**
   * *******************************
   * 获取随机数
   * 
   * 参数1=随机数长度
   * 
   */
  function gte_test_arr($int) {
    
      /**********取随机数字数组**************/
  $check_list = array('q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'l', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'z', 'z', 'x', 'c', 'v', 'b', 'n', 'm', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'I', 'P', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', 1, 2, 3, 4, 5, 6, 7,
    8, 9, 0);
    
    $test = '';
    shuffle($check_list); //打乱
    $ki = 0;
    while ($ki < $int) {
      $test .= $check_list[mt_rand(1, 61)];
      $ki++;
    }
    return $test;

  }
  
/**
 * 判断 文件/目录 是否可写（取代系统自带的 is_writeable 函数）
 *
 * @param string $file 文件/目录
 * @return boolean
 */
function new_is_writeable($file) {
	if (is_dir($file)){
		$dir = $file;
		if ($fp = @fopen("$dir/test.txt", 'w')) {
			@fclose($fp);
			@unlink("$dir/test.txt");
			$writeable = 1;
		} else {
			$writeable = 0;
		}
	} else {
		if ($fp = @fopen($file, 'a+')) {
			@fclose($fp);
			$writeable = 1;
		} else {
			$writeable = 0;
		}
	}

	return $writeable;
}


  /**
   * @信息框系统框
   * 默认返回前一页
   */
  function alert($NAME) {
    echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
    echo "<script language=javascript>alert('".$NAME."');</script>";
    echo '<script> var url =document.referrer; window.location=url; </script> ';
    exit;
  }

isset($_GET['check']) ?$check = $_GET['check'] : $check = null;
isset($_GET['step']) ? $step = $_GET['step'] : $step = null;

 /**
  * 判断是否安装过
  */

 if(file_exists($lujing.'/Data/install.txt') and $step!=3) {
    if( $step!=5) exit('<meta http-equiv="refresh" content="0;URL=install.php?step=3&s=1" />');
	  

 }


 
 

    isset($_POST['dbhost']) ? $dbhost = $_POST['dbhost'] : $dbhost = '127.0.0.1';
    isset($_POST['dbuser']) ? $dbuser = $_POST['dbuser'] : $dbuser = 'root';
    isset($_POST['dbpw']) ? $dbpw = $_POST['dbpw'] : $dbpw = '';
    isset($_POST['dbname']) ? $dbname = $_POST['dbname'] : $dbname = '';
    isset($_POST['dbtop']) ? $dbtop = $_POST['dbtop'] : $dbtop = 'hdsys_';
    isset($_POST['Coding']) ? $Coding = $_POST['Coding'] : $Coding = '0';
    isset($_POST['sqlitename']) ? $param_tmpitename = $_POST['sqlitename'] : $param_tmpitename = md5(time().rand(5, 100).rand(5, 8888).rand(5, 1999995)).'';
    isset($_POST['type']) ? $type = $_POST['type'] : $Coding = 'sqlite';
    //isset($_POST['sqlfile']) ? $param_tmpfile = $_POST['sqlfile'] : $param_tmpfile = 'sqlfile';

$file_check_error = 1;
if ($step == 1) //----------------------------------------系全新检测------------------------------------------
{
    $report = array();

    
    include_once ('data/fi_array.php');
    $ok=0;
    $url1='install.php?step=1';
    $a = count($arr_Afig);
    $i = 1;
    while ($i <= $a)
    {
        $file_check_report[$i][0] = $lujing.'/' . $arr_Afig[$i];
        $filename = $lujing.'/' . $arr_Afig[$i];
        if (file_exists($filename))
        {
            if (new_is_writeable($filename))
            {
                $file_check_report[$i][1] = '1';
                $filename_ok = '1';
                
				IF($ok==0)$ok = 2;
                
            } else
            {
                $file_check_report[$i][1] = '2';
                $ok=1;
            }
        } else
        {
            $file_check_report[$i][1] = '3';
            $ok=1;
        }
        $i++;
    }
    
    if($ok==2) $url1='install.php?step=2';
    
} elseif ($step == 2)
//---------------------------------------------安装数据库升级的和全新的---------------------------------------
{

   

    if (isset($_POST['submit']))
    {
        
if(PHP_VERSION == 5.2 ){

echo 'PHP版本过低无法使用！';
exit;
}
       
        
        /**
         * @拷贝文件
         */
        //dir_copy('PhpFile/LibBsphp'.$file_dir,'../LibBsphp/');
        //dir_copy('PhpFile/modules'.$file_dir,'../include/modules/');
        
        
        if($type=='mysql'){
			
			
		   $param_array = explode(':',$dbhost);
		  if(isset($param_array[1])){
			$dbhost =   $param_array[0];
			$port = $param_array[1];
		  }else{
			 $port=3306; 
		  }
			
           $link= mysqli_connect($dbhost, $dbuser, $dbpw, $dbname, $port) or die('数据库连接失败 IP地址、用户名或者密码错误 MYSQL错误信息:'.mysqli_error());
           mysqli_query($link, "set names 'utf8'");
        
		}else{
			
			
			
			$db_name = "../Data/{$param_tmpitename}.db";
			//创建数据库文件,文件内容为空
			if (!file_exists($db_name)) {
			 if (!($fp = fopen($db_name, "w+"))) {
			    exit('数据库创建失败');
			 }
			 fclose($fp);
			}
			
			
			
			
			try {
			    $link=new PDO('sqlite:'.$db_name);
			} catch (PDOException $e) {
			    try {
			        $link = new PDO('sqlite2:'.$db_name);
			    } catch (PDOException $e) {
			        exit('error!');
			    }
			}
			
		}

   
        /**
         * 写出配置文件
         */
     $ini='<?php 
     //以下是数据库配置信息
	 //南宁火蝶科技有限公司
	 //hdsyscms Inc
	 
	 
	 const DBSQLTYPE = "' . $type . '";      //数据库连接类型 sqlite/mysql
	 const DBSQLITEFILE = "' . $param_tmpitename . '.db";//sqlite数据库路径
	 
	 
	 
     const   DBHOST = "' . $dbhost . '";     //数据库服务器地址 127.0.0.1
     const   DBUSER = "' . $dbuser . '";     //数据库用户名
     const   DBPASS = "' . $dbpw . '";       //数据库密码
     const   DBTABLE = "' . $dbname . '";    //数据库名
     const   DBQIANHUAN = "' . $dbtop . '";  //表格缓 
     ?>';
     $param_tmp=file_put_contents('../Data/Sql.Config.php',$ini);
     if(!$param_tmp) die('配置保存失败,/Data/Sql.Config.php 不可写,请设置属性777可写,有相关疑问到 CHM.HDSYSCMS.COM 查询!,');
        
     
     
       /**
        * 读取数据库
        * 
        * hdsys.mysql.txt=结构
        */
        //结构
		if($type=='mysql'){
           $param_tmp = file_get_contents('./data/hdsys.mysql.txt');
		}else{
		   $param_tmp = file_get_contents('./data/hdsys.sqlite.txt');
		}
		
		
        $param_tmp = str_replace('hdsys_',$dbtop , $param_tmp);
        $param_tmparr=explode(";",$param_tmp);
        
        
        //数据
        $param_tmp = file_get_contents('./data/hdsys.data.txt');
        $param_tmp = str_replace('hdsys_',$dbtop , $param_tmp);
		

        
        //$BSPHP_HOSY_HOST_URL=str_replace('install/install.php?step=2' , '', strtolower(url()));
        //$param_tmp = str_replace('BSPHP_HOST_URL' ,$BSPHP_HOSY_HOST_URL , $param_tmp);
		
        $dataarr=explode(";",$param_tmp);
		
		
        //演示数据
        $param_tmp = file_get_contents('./data/hdsys.show.txt');
        $param_tmp = str_replace('hdsys_',$dbtop , $param_tmp);
        $showarr=explode(";",$param_tmp);
		
		$bad = 0;
		
        
        //结构
        for($i=0;$i<count($param_tmparr)-1;$i++){
			
			if(trim($param_tmparr[$i])!=''){
				if($type=='mysql'){
				   $tmp = mysqli_query($link,trim($param_tmparr[$i]));
				}else{
				   
				   try {
				      $tmp = $link->query(trim($param_tmparr[$i]).';');
				   } catch (PDOException $e) {
				       exit('error!'.$e);
				   }
			
				   
				   
				}
				
				if(!$tmp){
					echo "执行失败TABLE:".$param_tmparr[$i].';<br/>';
					$bad = 1;
				}
			}
			
			
     	}
        

        //数据
        for($i=0;$i<count($dataarr)-1;$i++){
			
			if(trim($dataarr[$i])!=''){
				if($type=='mysql'){
				   $tmp = mysqli_query($link,trim($dataarr[$i]));
				}else{
				   $tmp = $link->query(trim($dataarr[$i]));
				   
				}
				
				if(!$tmp){
					echo "执行失败Daa:".$dataarr[$i].';<br/>';
					$bad = 1;
				}
			
			
			}
     	}
		
		
        //存数据
		for($i=0;$i<count($showarr)-1;$i++){
			
			if(trim($showarr[$i])!=''){
				if($type=='mysql'){
				   $tmp = mysqli_query($link,trim($showarr[$i]));
				}else{
				   $tmp = $link->query(trim($showarr[$i]));
				   
				}
				
				if(!$tmp){
					echo "执行失败Show:".$showarr[$i].';<br/>';
					$bad = 1;
				}
			
			
			}
		}
        
       $param_tmp=file_put_contents($lujing.'/Data/install.txt',time());
	   if($bad ==0){
          echo '<meta http-equiv="refresh" content="0;URL=install.php?step=5" />';
	   }

  }


}elseif ($step == 3)
//---------------------------------------------安装数据库升级的和全新的---------------------------------------
{

 if(!file_exists($lujing.'/Data/install.txt')) {
    if( $step!=5) exit('<meta http-equiv="refresh" content="0;URL=index.php" />');
	  

 }



}
include_once ('template/install.php');

?>